#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File  :repair_pdf.py
# Time  :2025/4/18 14:07
# Email :fortunatedong@163.com
# Author:by Adonli
import os

import pandas as pd
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

from config import ROOT_DIR


class InputRepairBill():
    def __init__(self,sort_number,date,model,serial_number,userame_text,contact_person,address,postcode,fixed_number,fax_number,optional_parts,profession,hr_repair_pdf_dir):
        self.sort_number = sort_number
        self.date = date
        self.model = model
        self.serial_number = serial_number
        self.userame_text = userame_text
        self.contact_person = contact_person
        self.address = address
        self.postcode = postcode
        self.fixed_number = fixed_number
        self.fax_number = fax_number
        self.optional_parts = optional_parts
        self.profession = profession
        self.year = date[:4]
        self.month = date[4:6]
        self.day = date[6:]
        self.hr_repair_pdf_dir = hr_repair_pdf_dir

    def input_message(self):
        pdfmetrics.registerFont(TTFont('微软雅黑 粗体', 'C:/Windows/Fonts/msyhbd.ttc'))
        pdf_name = f"{self.sort_number}-{self.model}-{self.serial_number}-{self.date}.pdf"
        hr_repair_pdf_path = f"{self.hr_repair_pdf_dir}/{pdf_name}"
        c = canvas.Canvas(hr_repair_pdf_path, pagesize=A4)
        c.setFont("微软雅黑 粗体", 9)
        # 年月日
        c.drawString(15.4 * cm, 23.9 * cm, self.year)
        c.drawString(17 * cm, 23.9 * cm, self.month)
        c.drawString(18.1 * cm, 23.9 * cm, self.day)

        # 用户名称
        userame_text = self.userame_text
        # print(len(userame_text))
        if len(userame_text) > 17:
            c.setFont("微软雅黑 粗体", 8)
            c.drawString(4.7 * cm, 14.1 * cm, userame_text)
            c.setFont("微软雅黑 粗体", 9)
        else:
            c.drawString(4.7 * cm, 14.1 * cm, userame_text)
        # c.drawString(4.7 * cm, 14.2 * cm, userame_text)

        # 联系人
        contact_person = self.contact_person
        c.drawString(11.6 * cm, 14.1 * cm, contact_person)

        # 地址
        address = self.address
        # print(len(address))
        if len(address) > 27:
            c.setFont("微软雅黑 粗体", 8)
            c.drawString(4.2 * cm, 13.5 * cm, address)
            c.setFont("微软雅黑 粗体", 9)
        else:
            c.drawString(4.2 * cm, 13.5 * cm, address)
        # c.drawString(4.2 * cm, 13.55 * cm, address)

        # 邮编
        postcode = self.postcode
        c.drawString(13.8 * cm, 13.5 * cm, "")

        # 固定电话号码
        fixed_number = self.fixed_number
        c.drawString(5.4 * cm, 12.9 * cm, fixed_number)

        # 传真号码
        fax_number = self.fax_number
        c.drawString(11 * cm, 12.9 * cm, fax_number)

        # 年
        c.drawString(5.8 * cm, 12.2 * cm, self.year)
        # 月
        c.drawString(7.35 * cm, 12.2 * cm, self.month)
        # 日
        c.drawString(8.35 * cm, 12.2 * cm, self.day)
        # 选购件
        optional_parts = self.optional_parts
        c.drawString(4.6 * cm, 11.8 * cm, optional_parts)
        # 行业
        profession = self.profession
        c.drawString(4.8 * cm, 11.2 * cm, profession)

        # 经销商名称
        # dealer_name = "广州金穗隆信息科技股份有限公司"
        c.drawString(5.3 * cm, 7.45 * cm, "广州金穗隆信息科技股份有限公司")
        # 电话号码
        # dealer_number = "202-66608000"
        c.drawString(5.0 * cm, 6.85 * cm, "202-66608000")
        # 传真号码
        # dealer_fax_number = "020-62324733"
        c.drawString(10.2 * cm, 6.85 * cm, "020-62324733")
        # 维修店
        # repair_name = "广州金穗隆信息科技股份有限公司"
        c.drawString(4.6 * cm, 5.35 * cm, "广州金穗隆信息科技股份有限公司")
        # 电话号码
        # repair_number = "202-66608000"
        c.drawString(5.3 * cm, 4.75 * cm, "202-66608000")
        # 传真号码
        # rapair_fax_number = "020-62324733"
        c.drawString(10.2 * cm, 4.75 * cm, "020-62324733")
        # 安装工程师
        # installation_engineer = "陈工、张工"
        c.drawString(5.2 * cm, 4.15 * cm, "陈工、张工")
        c.save()
        return pdf_name


class RepairBillPDF():
    def __init__(self,repair_file_path):
        self.repair_file_path = repair_file_path
        self.hr_repair_pdf_dir = os.path.join(ROOT_DIR, 'Datas/hr_repair_pdf/repair_pdf_result')
    def load_repair_file(self):
        df = pd.read_excel(self.repair_file_path,dtype=str)
        return df
    def generate_repair_pdf(self):
        df = self.load_repair_file()
        pdf_names = []
        for index,row in df.iterrows():
            sort_number = index
            date = row["交货/安装日期"]
            model = row["机型（规格型号）"]
            serial_number = row["机身号（序列号）"]
            userame_text = row["客户名称"]
            contact_person = row["联系人"]
            address = row["联系地址"]
            postcode = ""
            iphone_number = row["电话"]
            optional_parts = ""
            profession = ""
            # print(iphone_number[:1])
            if len(iphone_number) == 11 and iphone_number[:1]=="1":
                fixed_number = iphone_number
                fax_number = ""
            else:
                fixed_number = ""
                fax_number = iphone_number
            print(f"sort_number:{sort_number},date:{date},model:{model},serial_number:{serial_number},userame_text:{userame_text},"
                  f"contact_person:{contact_person},address:{address},postcode:{postcode},optional_parts:{optional_parts},"
                  f"profession:{profession},fixed_number:{fixed_number},fax_number:{fax_number},"
                    )
            input_repair_bill = InputRepairBill(sort_number,date,model,serial_number,userame_text,contact_person,address,postcode,fixed_number,fax_number,optional_parts,profession,self.hr_repair_pdf_dir)
            pdf_name = input_repair_bill.input_message()
            # print(pdf_name)
            pdf_names.append(pdf_name)

        return pdf_names

# if __name__ == '__main__':
#     input_repair_pdf = RepairBillPDF('C:/Users/yadong_li/Desktop/人事/保单.xlsx')
#     input_repair_pdf.generate_repair_pdf()
